extends Node2D

# 水面着色器材质
@onready var water_shader = $WaterShader.material

# 交互贴图
var interaction_texture: ImageTexture
var interaction_image: Image

# 玩家和石头容器
@onready var player = $Player
@onready var stone_container = $StoneContainer

# 配置
var player_influence_radius = 0.05
var stone_influence_radius = 0.08

func _ready():
	# 创建交互贴图
	var viewport_size = get_viewport_rect().size
	interaction_image = Image.create(viewport_size.x, viewport_size.y, false, Image.FORMAT_RGBA8)
	interaction_texture = ImageTexture.create_from_image(interaction_image)
	
	# 设置到着色器
	water_shader.set_shader_parameter("interaction_map", interaction_texture)
	
	# 创建噪声纹理
	create_noise_textures()

func _process(delta):
	# 更新交互贴图
	update_interaction_map()

func update_interaction_map():
	# 清除上一帧数据
	interaction_image.fill(Color(0, 0, 0, 0))
	
	# 绘制玩家影响
	draw_player_influence()
	
	# 绘制石头影响
	draw_stones_influence()
	
	# 更新纹理
	interaction_texture.update(interaction_image)

func draw_player_influence():
	var player_pos = player.position
	var player_global_pos = to_local(player_pos)
	
	# 计算UV坐标
	var viewport_size = get_viewport_rect().size
	var uv_x = player_global_pos.x / viewport_size.x
	var uv_y = player_global_pos.y / viewport_size.y
	
	# 根据速度计算影响强度
	var velocity = player.velocity.length()
	var influence_strength = clamp(velocity / 100.0, 0.1, 1.0)
	
	# 在玩家位置绘制圆形影响区域
	draw_circle_on_texture(
		Vector2(uv_x, uv_y), 
		player_influence_radius, 
		Color(influence_strength, 0, 0, 0)
	)

func draw_stones_influence():
	for stone in stone_container.get_children():
		var stone_global_pos = to_local(stone.position)
		
		# 计算UV坐标
		var viewport_size = get_viewport_rect().size
		var uv_x = stone_global_pos.x / viewport_size.x
		var uv_y = stone_global_pos.y / viewport_size.y
		
		# 在石头位置绘制圆形影响区域
		draw_circle_on_texture(
			Vector2(uv_x, uv_y), 
			stone_influence_radius, 
			Color(0, stone.get_meta("influence", 1.0), 0, 0)
		)

func draw_circle_on_texture(center: Vector2, radius: float, color: Color):
	var tex_size = interaction_image.get_size()
	var px_center = Vector2(center.x * tex_size.x, center.y * tex_size.y)
	var px_radius = radius * tex_size.x
	
	# 简单的圆形绘制
	for y in range(-px_radius, px_radius):
		for x in range(-px_radius, px_radius):
			var dist = Vector2(x, y).length()
			if dist < px_radius:
				var alpha = 1.0 - smoothstep(px_radius * 0.8, px_radius, dist)
				var draw_color = color
				draw_color.a *= alpha
				
				var px_x = clamp(px_center.x + x, 0, tex_size.x - 1)
				var px_y = clamp(px_center.y + y, 0, tex_size.y - 1)
				
				var existing_color = interaction_image.get_pixel(px_x, px_y)
				var blended_color = blend_colors(existing_color, draw_color)
				interaction_image.set_pixel(px_x, px_y, blended_color)

func blend_colors(a: Color, b: Color) -> Color:
	# 简单叠加混合
	return Color(
		max(a.r, b.r),
		max(a.g, b.g),
		max(a.b, b.b),
		max(a.a, b.a)
	)

func create_noise_textures():
	# 创建水波噪声
	var water_noise = FastNoiseLite.new()
	water_noise.noise_type = FastNoiseLite.TYPE_PERLIN
	water_noise.frequency = 0.1
	water_noise.fractal_octaves = 3
	
	var water_noise_texture = NoiseTexture2D.new()
	water_noise_texture.width = 512
	water_noise_texture.height = 512
	water_noise_texture.noise = water_noise
	water_shader.set_shader_parameter("water_noise", water_noise_texture)
	
	# 创建水花噪声
	var splash_noise = FastNoiseLite.new()
	splash_noise.noise_type = FastNoiseLite.TYPE_CELLULAR
	splash_noise.frequency = 0.5
	splash_noise.fractal_octaves = 2
	
	var splash_noise_texture = NoiseTexture2D.new()
	splash_noise_texture.width = 512
	splash_noise_texture.height = 512
	splash_noise_texture.noise = splash_noise
	water_shader.set_shader_parameter("splash_noise", splash_noise_texture)

func add_stone(position: Vector2, size: float = 1.0):
	var stone = Sprite2D.new()
	stone.texture = preload("res://assets/stone.png")
	stone.position = position
	stone.scale = Vector2(size, size)
	
	# 设置石头属性
	stone.set_meta("is_stone", true)
	stone.set_meta("influence", clamp(size, 0.5, 2.0))
	
	stone_container.add_child(stone)
	return stone
